<template>
  <div>
    <vab-card>
      <template #header>
        <span class="leve_title">底价价目表</span>
      </template>
      <template #default>
        <el-form
          :ref="(el) => (searchFormRef = el)"
          :model="searchForm"
          label-width="100px"
          label-position="left"
          class="form_search"
        >
          <el-row :gutter="24">
            <el-col
              v-for="(item, i) in StandardPriceSearchFor"
              :key="i"
              :xs="24"
              :sm="12"
              :md="8"
              :lg="6"
              :xl="6"
            >
              <el-form-item
                v-if="item.type == 'select'"
                :label="item.label"
                :prop="item.prop"
              >
                <el-select
                  clearable
                  class="width_100 height_auto"
                  :multiple="item.multiple"
                  :filterable="item.multiple"
                  v-model="searchForm[item.prop]"
                  @keyup.enter.native="onkeydown"
                >
                  <el-option
                    clearable
                    v-for="(jtem, j) in [{ value: '0', label: '否' }, { value: '1', label: '是' }]"
                    :key="jtem.value"
                    :label="jtem.label"
                    :value="jtem.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item
                v-if="item.type == 'lazySelect'"
                :label="item.label"
                :prop="item.prop"
              >
                <ProductSelect v-model.trim="searchForm[item.prop]" :type="item.selectType" @keyup.enter.native="onkeydown"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <div class="flex_just_end">
                <el-button size="small" type="primary" @click="handleSearch()">
                  查询
                </el-button>
                <el-button size="small" @click="resetForm(searchFormRef)">
                  重置
                </el-button>
              </div>
            </el-col>
          </el-row>
        </el-form>
      </template>
    </vab-card>
    <vab-card shadow="always">
      <template #header>
        <span class="leve2_title">明细列表</span>
        <!-- <div class="flex_just_end">
          <VabUploadFile :query="{ type: 2 }" @Refresh="() => {
            ElMessage({
              message: '导入成功',
              type: 'success',
            })
            _GetProductList()
          }"/>
        </div> -->
      </template>
      <el-table
        border
        height="450"
        class="column"
        style="width: 100%"
        :data="data.dataSource"
        v-loading="data.loading"
      >
        <el-table-column label="序号" align="center" width="80">
          <template #default="scope">
            <span
              v-text="(data.pageNum - 1) * data.pageSize + (scope.$index + 1)"
            ></span>
          </template>
        </el-table-column>
        <el-table-column
          v-for="(item, i) in data.cloums"
          :key="i"
          :width="item.width"
          :prop="item.prop"
          :label="item.label"
          :align="item.type == 'money' || item.type == 'number' ? 'right' : 'center'"
        >
          <template #default="scope">
            <!-- 正常 -->
            <span 
              v-if="!item.type"
              class="ellipsis"
            >
              {{ _FieldFormat(scope.row[item.prop]) }}
            </span>
            <!-- 状态 -->
            <span 
              v-if="item.type == 'boolean'"
              class="ellipsis"
            >
              <el-tag v-if="scope.row[item.prop] == 1" class="ml-2" type="success">是</el-tag>
              <el-tag v-else class="ml-2" type="warning">否</el-tag>
            </span>
            <!-- 加密 -->
            <span v-if="item.type == 'money_eye'" class="flex_just_center" @click="() => {
              scope.row.disabled = !scope.row.disabled
            }">
              {{ _priceEncryption(scope.row[item.prop], scope.row.disabled) }}
              <vab-icon icon="eye-line" v-if="scope.row.disabled"/>
              <vab-icon icon="eye-close-line" v-else/>
            </span>
          </template>
        </el-table-column>

        <!-- 操作 -->
        <el-table-column
          fixed="right"
          align="center"
          label="操作"
          width="80"
        >
          <template #default="scope">
            <el-button
              @click="handleEdit(scope.$index, scope.row)"
              size="small"
              class="button_update"
            >
              更新
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex_just_end">
        <el-pagination
          background
          small="small"
          v-model:current-page="data.pageNum"
          v-model:page-size="data.pageSize"
          layout="total, prev, pager, next,sizes, jumper"
          :page-sizes="[20, 50, 100, 200]"
          :pager-count="10"
          :total="data.count"
          @current-change="handlePageNum"
          @size-change="handlePageSize"
        />
      </div>
    </vab-card>
    <PriceDialog 
      type="2"
      v-if="currentRow"
      :dialog-visible="data.dialogVisible" 
      :currentRow="currentRow" 
      @refresh="_GetProductList"
      @handle-close="handleClose"
    />
   
  </div>
</template>

<script lang="ts" setup>
defineOptions({
  name: 'ProductStandardPrice',
})
import { ElMessage, FormInstance } from 'element-plus'
/**
 * @exports
 */
import {
  InfoData,
  StandardPriceSearchFor,
  LowPriceCloums,
  ExportLowXlsx,
  InfoSearchSelect,
} from './components/tools'
import { FieldFormat } from '~/src/utils/utils'
import { getProductSearch, productPriceList, productImport } from '~/src/api/dms/product'
import PriceDialog from './components/PriceDialog.vue'
import ProductSelect from '/@/components/lazySelect/productSelect.vue'

/**
 * @interface
 */
interface dataFace {
  dataSource: any[]
  cloums: any[]
  pageNum: number
  pageSize: number
  count: number
  search: any
  loading: boolean
  dialogVisible: boolean
}
/**
 * @var
 */
const router = useRouter()
const currentRow = ref<any>()
const searchFormRef = ref<any>(null)
const searchForm = reactive<any>({})
const data: dataFace = reactive<any>({
  dataSource: [],
  cloums: [],
  pageNum: 1,
  pageSize: 20,
  count: 0,
  search: {},
  loading: false,
  dialogVisible: false
})
data.dataSource = InfoData
data.cloums = LowPriceCloums

/**
 * @method
 */
// 金额加密
const _priceEncryption = (value: string, show: boolean) => {
  return show ? value : '***'
}
const selectOptions = (value: string) => {
  if (JSON.stringify(data.search) == '{}') return
  return InfoSearchSelect(data.search, value)
}

const handleSearch = async (value = '') => {
  await _GetProductList()
  ElMessage({
    message: '查询成功',
    type: 'success',
  })
}
const resetForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.resetFields()
  await _GetProductList()
  ElMessage({
    message: '重置成功',
    type: 'success',
  })
}

const onkeydown = (value: any) => {
  _GetProductList()
}

const handlePageNum = (value: any) => {
  data.pageNum = value
  _GetProductList()
}
const handlePageSize = (value: any) => {
  data.pageSize = value
  _GetProductList()
}
const handleClick = (value: string, prop = {}) => {
  // 导入
  if (value == 'import') {
    productImport({
      type: 2,
      path: ExportLowXlsx,
    }).then(async res => {
      await ElMessage({
        message: '导入成功',
        type: 'success',
      })
      _GetProductList()
    })
    return
  }
}
// 更新
const handleEdit = async (index: number, row: any) => {
  row.time = null
  if (row.validity_start && row.validity_end) {
    row.time = await [row.validity_start, row.validity_end]
  }
  currentRow.value = { ...row }
  data.dialogVisible = true
}
const handleEncryption = (index: number, item: any) => {
  item.disabled = !item.disabled
}
// 关闭弹窗
const handleClose = (value: boolean) => {
  data.dialogVisible = false
}

// 字段级格式化
const _FieldFormat = (value: string, type = '') => {
  return FieldFormat(value, type)
}
const _GetProductList = async () => {
  data.loading = true
  const query = {
    page: data.pageNum,
    limit: data.pageSize,
    type: 2, //固定必传，1底价价目表，2底价价目表
    filter: {
      goods_name: searchForm.goods_name || '', // 产品名称
      goods_number: searchForm.goods_number || '', // 产品编码
      is_validity: searchForm.is_validity || '', // 是否有效 0无效1有效
    },
  }
  try {
    const res = await productPriceList({ ...query })
    data.dataSource = res.data.list.map((item: any) => {
      return {
        ...item,
        disabled: false
      }
    })
    data.count = res.data.count
    data.loading = false
  } catch (error) { }
}

const _GetProductSearch = async () => {
  try {
    const res = await getProductSearch()

    res.data.brand.push({ entry_id: '', name: '空' })
    res.data.class_one.push({ id: '', data_title: '空' })
    res.data.class_two.push({ id: '', data_title: '空' })
    res.data.series.push({ id: '', data_title: '空' })
    res.data.category.push({ id: '', data_title: '空' })

    data.search = res.data
  } catch (error) { }
}

onMounted(() => {
  _GetProductList()
  _GetProductSearch()
})
</script>

<style lang="scss" scoped>
@import '../../components/public.scss';

.column {
  :deep() {
    .el-table_1_column_12 {
      width: 108px;
    }
  }
}
</style>
